package com.linewell.commom.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linewell.commom.dao.ISecondMarketDao;
import com.linewell.commom.model.SecondMarket;
import com.linewell.commom.service.ISecondMarketService;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.utils.HqlHelper;
@Service
public class SecondMarketServiceImpl implements ISecondMarketService{
	@Autowired
	private ISecondMarketDao secondMarketDao;

	@Override
	public DataGrid datagrid(PageHelper ph, SecondMarket s, Integer depId) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		List<SecondMarket> cml = new ArrayList<SecondMarket>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SecondMarket t where 1=1 ");
		//以下为查询条件
		if (null != s.getSysDepartment()  && s.getSysDepartment().getDepId() != 0) {
				params.put("depId", s.getSysDepartment().getDepId());
				hql.append(" and t.sysDepartment.depId = :depId ");
			}
		if(s.getFlag() != null && !"".equals(s.getFlag())){
			params.put("flag", s.getFlag().byteValue());
			hql.append(" and t.flag = :flag ");
		}
		if(s.getTitle() != null && !"".equals(s.getTitle())){
			params.put("title", s.getTitle());
			hql.append(" and t.title = :title ");
		}
		if(s.getSummary() != null && !"".equals(s.getSummary())){
			params.put("summary", s.getSummary());
			hql.append(" and t.summary = :summary ");
		}
		if(s.getPhoto() != null && !"".equals(s.getPhoto())){
			params.put("photo", s.getPhoto());
			hql.append(" and t.photo = :photo ");
		}
		if(s.getStatus() != null && !"".equals(s.getStatus())){
				params.put("status", s.getStatus().byteValue());
				hql.append(" and t.status = :status ");
			}
		if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId("+depId+")) > 0 ");
		}
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SecondMarket> l = secondMarketDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(secondMarketDao.count("select count(*) " + hql, params));
		return dg;
	}

	@Override
	public boolean save(SecondMarket secondMarket) {
		// TODO Auto-generated method stub
		secondMarketDao.save(secondMarket);
		return true;
	}

	@Override
	public boolean update(SecondMarket secondMarket) {
		// TODO Auto-generated method stub
		secondMarketDao.update(secondMarket);
		return true;
	}

	@Override
	public boolean deleteAll(String ids) {
		// TODO Auto-generated method stub
		secondMarketDao.deleteAll(ids);
		return true;
	}

	@Override
	public boolean removeAll(String ids) {
		// TODO Auto-generated method stub
		secondMarketDao.removeAll(ids);
		return true;
	}

	@Override
	public List<SecondMarket> getUser(String id) {
		// TODO Auto-generated method stub
		String hql = "from SecondMarket s where s.marketId="+id;
		List<SecondMarket> s = secondMarketDao.find(hql);
		return s;
	}
}
